package com.wy.panda.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis相关自定义配置
 *
 * @author WangYuan
 * @since 2020年9月10日 下午7:19:07
 */
@Configuration
public class RedisConfig {
	
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;
	
	@PostConstruct
	public void config() {
		redisTemplate.setEnableDefaultSerializer(false);
		
		redisTemplate.setKeySerializer(RedisSerializer.string());
		redisTemplate.setValueSerializer(RedisSerializer.json());
		redisTemplate.setHashKeySerializer(RedisSerializer.string());
		redisTemplate.setHashValueSerializer(RedisSerializer.json());
		redisTemplate.setStringSerializer(RedisSerializer.string());
	}
	
	@Bean("valueOperations")
	public ValueOperations<Object, Object> valueOperations() {
		return redisTemplate.opsForValue();
	}
	
	@Bean("listOperations")
	public ListOperations<Object, Object> listOperations() {
		return redisTemplate.opsForList();
	}
	
	@Bean("hashOperations")
	public HashOperations<Object, Object, Object> hashOperations() {
		return redisTemplate.opsForHash();
	}
	
	@Bean("setOperations")
	public SetOperations<Object, Object> setOperations() {
		return redisTemplate.opsForSet();
	}
	
	@Bean("zsetOperations")
	public ZSetOperations<Object, Object> zsetOperations() {
		return redisTemplate.opsForZSet();
	}
}
